﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ScottCateAjax2008.Lesson09
{
    public partial class A_HistoryServer : Page
    {
        protected override void OnInit(EventArgs e)
        {
            BasicGridView1.TheGrid.PageIndexChanging += TheGrid_PageIndexChanging;
            BasicGridView1.TheGrid.Sorting += TheGrid_Sorting;

            //Navigate Event
            ScriptManager1.Navigate += ScriptManager1_Navigate;
            base.OnInit(e);
        }

        #region Grid Page Sort and Index Changed - Add History Point + Title


        private void TheGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (ScriptManager1.EnableHistory)
            {
                string expression = ((GridView)sender).SortExpression;
                SortDirection direction = ((GridView)sender).SortDirection;

                string title = string.Format("Grid Page : {0}, Sorted by {1} in {2} order",
                                             e.NewPageIndex + 1, expression, direction);
                ScriptManager1.AddHistoryPoint("GridPage", e.NewPageIndex.ToString(), title);

                string state = string.Format("{0}|{1}", expression, direction);
                ScriptManager1.AddHistoryPoint("GridSort", state, title);
                Page.Title = title;
            }
        }

        private void TheGrid_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (ScriptManager1.EnableHistory && !ScriptManager1.IsNavigating)
            {
                //string currentPage = ((GridView) sender).PageIndex.ToString();
                //currentPage is always 1 when starting a new sort
                string currentPage = "1";
                string state = string.Format("{0}|{1}", e.SortExpression, e.SortDirection);
                string title = string.Format("Sorting {0} in {1} order, Page {2}",
                                              e.SortExpression, e.SortDirection, currentPage);
                ScriptManager1.AddHistoryPoint("GridSort", state, title);
                ScriptManager1.AddHistoryPoint("GridPage", currentPage, title);
                Page.Title = title;
            }
        }

        #endregion

        #region Script Manager Navigation

        private void ScriptManager1_Navigate(object sender, HistoryEventArgs e)
        {
            string gridSort = e.State["GridSort"];
            string gridPage = e.State["GridPage"];

            string colName = "ProductId";
            SortDirection direction = SortDirection.Ascending;

            if (!string.IsNullOrEmpty(gridSort))
            {
                string[] state = gridSort.Split( '|' );
                colName = state[0];
                direction = (SortDirection) Enum.Parse( typeof( SortDirection ), state[1] );
            }
            BasicGridView1.TheGrid.Sort(colName, direction);

            if (!String.IsNullOrEmpty(gridPage))
            {
                BasicGridView1.TheGrid.PageIndex = Convert.ToInt32(gridPage);
            }
            else
                BasicGridView1.TheGrid.PageIndex = 0;

            //Reset Page Title
            Page.Title = string.Format("Page {0}, Sorting {1} {2}", 
                BasicGridView1.TheGrid.PageIndex, colName, direction);
        }

        #endregion
    }
}


















